﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using stdole;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System.Drawing;

namespace hzGis.service.impl
{
    public class StyleService: IStyleService
    {
        public void setDLZWBStyle(ILayer layer)
        {
            IColor pLineColor =new RgbColor();
            ICartographicLineSymbol pCartoLineSymbol =new CartographicLineSymbol();
            pCartoLineSymbol.Width = 0.4;
            
            pCartoLineSymbol.Color = pLineColor;

            //创建一个填充符号

            ISimpleFillSymbol pSmplFillSymbol =new SimpleFillSymbol();

            //设置填充符号的属性

            IColor pRgbClr =new RgbColor();
            pRgbClr.Transparency = 0;

            IFillSymbol pFillSymbol = pSmplFillSymbol;

            pFillSymbol.Color = pRgbClr;

            pFillSymbol.Outline = pCartoLineSymbol;
            IGeoFeatureLayer pGeoFeatureLayer = layer as IGeoFeatureLayer;
            IFeatureRenderer pFeatureRenderer = pGeoFeatureLayer.Renderer;
            ISimpleRenderer pSimpleRenderer = pFeatureRenderer as ISimpleRenderer;
            pSimpleRenderer.Symbol = pFillSymbol as ISymbol;

        }

        public void setStyle(ILayer layer, byte[] LineColor, double LineWidth, byte[] fillColor)
        {
            IColor pLineColor = new RgbColor() {
                Red = LineColor[0],
                Green = LineColor[1],
                Blue = LineColor[2]
            };
            ICartographicLineSymbol pCartoLineSymbol = new CartographicLineSymbol();
            pCartoLineSymbol.Width = LineWidth;
            pCartoLineSymbol.Color = pLineColor;
            //创建一个填充符号
            ISimpleFillSymbol pSmplFillSymbol = new SimpleFillSymbol();
            IColor pRgbClr = new RgbColor() {
                Red = fillColor[0],
                Green = fillColor[1],
                Blue = fillColor[2]
            };
            pRgbClr.Transparency = fillColor[3];
            IFillSymbol pFillSymbol = pSmplFillSymbol;
            pFillSymbol.Color = pRgbClr;
            pFillSymbol.Outline = pCartoLineSymbol;
            IGeoFeatureLayer pGeoFeatureLayer = layer as IGeoFeatureLayer;
            IFeatureRenderer pFeatureRenderer = pGeoFeatureLayer.Renderer;
            ISimpleRenderer pSimpleRenderer = pFeatureRenderer as ISimpleRenderer;
            pSimpleRenderer.Symbol = pFillSymbol as ISymbol;
        }

        public void setLabelStylePro(ILayer layer, string fieldName, byte[]fontColor, string fontFamily, bool isBold, int fontSize, long minScale, long maxScale)
        {
            IGeoFeatureLayer pGeoLayer = layer as IGeoFeatureLayer;
            IAnnotateLayerPropertiesCollection IPALPColl = pGeoLayer.AnnotationProperties;
            IPALPColl.Clear();
            IRgbColor pColor = new RgbColor()
            {
                Red = fontColor[0],
                Blue = fontColor[1],
                Green = fontColor[2]
            };
            IFontDisp pFont = new StdFont()
            {
                Name = fontFamily,
                Bold = isBold,
                Size = fontSize
            } as IFontDisp;

            ITextSymbol pTextSymbol = new TextSymbol()
            {
                Color = pColor,
                Font = pFont,
                Size = fontSize
            };
            //用来控制标注和要素的相对位置关系
            ILineLabelPosition pLineLpos = new LineLabelPosition()
            {
                Parallel = false,  //修改标注的属性
                Perpendicular = true,
                InLine = true
            };
            //用来控制标注冲突
            ILineLabelPlacementPriorities pLinePlace = new LineLabelPlacementPriorities()
            {
                AboveStart = 5, //让above 和start的优先级为5
                BelowAfter = 4
            };
            //用来实现对ILineLabelPosition 和 ILineLabelPlacementPriorities以及更高级属性的控制
            IBasicOverposterLayerProperties pBOLP = new BasicOverposterLayerProperties()
            {
                FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon,
                LineLabelPlacementPriorities = pLinePlace,
                LineLabelPosition = pLineLpos
            };

            //创建标注对象
            ILabelEngineLayerProperties pLableEngine = new LabelEngineLayerProperties() as ILabelEngineLayerProperties;
            pLableEngine.Symbol = pTextSymbol;
            //pLableEngine.BasicOverposterLayerProperties = pBOLP;
            pLableEngine.IsExpressionSimple = true;
            pLableEngine.Expression = "[" + fieldName + "]";

            //设置标注的参考比例尺
            //IAnnotateLayerTransformationProperties pAnnoLyrPros = pLableEngine as IAnnotateLayerTransformationProperties;
           // pAnnoLyrPros.ReferenceScale = 2500000;

            //设置标注可见的最大最小比例尺
            IAnnotateLayerProperties pAnnoPros = pLableEngine as IAnnotateLayerProperties;
            pAnnoPros.AnnotationMaximumScale = minScale;
            pAnnoPros.AnnotationMinimumScale = maxScale;
            //pAnnoPros.WhereClause属性  设置过滤条件

            IPALPColl.Add(pAnnoPros);
            pGeoLayer.DisplayAnnotation = true;

        }

        public void setLabelStyle(ILayer layer, string fieldName)
        {
            ITextSymbol textSymbol = new TextSymbol();
            IColor ic = new RgbColor();
            ic.RGB = 0 * 65536 + 0 * 256 + 0;
            textSymbol.Color = ic;

            stdole.IFontDisp tempFont = new stdole.StdFont() as stdole.IFontDisp;
            tempFont.Name = "宋体";
            tempFont.Size = 8;
            textSymbol.Font = tempFont;
            textSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
            FunLabelFeatureLayer((IFeatureLayer)layer, fieldName, null, textSymbol);
        }

        //标注图层
        public void FunLabelFeatureLayer(IFeatureLayer pFeaturelayer, string sLableField, IRgbColor pRGB, ITextSymbol textSymbol)
        {
            //判断图层是否为空
            if (pFeaturelayer == null)
                return;
            IGeoFeatureLayer pGeoFeaturelayer = (IGeoFeatureLayer)pFeaturelayer;
            IAnnotateLayerPropertiesCollection pAnnoLayerPropsCollection;
            pAnnoLayerPropsCollection = pGeoFeaturelayer.AnnotationProperties;
            pAnnoLayerPropsCollection.Clear();

            //未指定字体颜色则默认为黑色
            if (pRGB == null)
            {
                pRGB = new RgbColor();
                pRGB.Red = 0;
                pRGB.Green = 0;
                pRGB.Blue = 0;
            }

            IBasicOverposterLayerProperties pBasicOverposterlayerProps = new BasicOverposterLayerProperties();
            switch (pFeaturelayer.FeatureClass.ShapeType)//判断图层类型
            {
                case esriGeometryType.esriGeometryPolygon:
                    pBasicOverposterlayerProps.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
                    break;
                case esriGeometryType.esriGeometryPoint:
                    pBasicOverposterlayerProps.FeatureType = esriBasicOverposterFeatureType.esriOverposterPoint;
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    pBasicOverposterlayerProps.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolyline;
                    break;
            }

               
            
            ILabelEngineLayerProperties pLabelEnginelayerProps = new LabelEngineLayerProperties() as ILabelEngineLayerProperties;
            pLabelEnginelayerProps.Expression = "[" + sLableField + "]";
            pLabelEnginelayerProps.Symbol = textSymbol;
            pBasicOverposterlayerProps.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerPart;
            pLabelEnginelayerProps.BasicOverposterLayerProperties = pBasicOverposterlayerProps;
            
            pAnnoLayerPropsCollection.Add((IAnnotateLayerProperties)pLabelEnginelayerProps);
            pGeoFeaturelayer.DisplayAnnotation = true;
        }

        /// <summary>
        /// 获取待要素类的所有属性字段名
        /// </summary>
        /// <param name="pFeatureClass">待复制要素类</param>
        /// <returns>返回待复制要素类的所有属性字段名</returns>
        public static List<string> get_FieldsString(IFeatureClass pFeatureClass)
        {
            IFields pFields = pFeatureClass.Fields;
            IField pField;
            List<string> s = new List<string>();
            for (int i = 0; i < pFields.FieldCount; i++)
            {
                pField = pFields.Field[i];
                if (pField.Type != esriFieldType.esriFieldTypeGeometry)
                    s.Add(pField.Name);
            }
            return s;
        }

        /// <summary>
        /// Color转IColor
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static IColor ColorToIColor(Color color)
        {
            IColor pColor = new RgbColor();
            pColor.RGB = color.B * 65536 + color.G * 256 + color.R;
            return pColor;
        }


        public void setStylePro(ILayer layer, byte[] LineColor, double LineWidth, byte[] fillColor, string field, List<string> bsms)
        {
            IFeatureLayer pFLayer = layer as IFeatureLayer;
            IGeoFeatureLayer geoLayer = layer as IGeoFeatureLayer;
            IFeatureClass fcls = pFLayer.FeatureClass;
            //IQueryFilter pqf = new QueryFilter();
            //IFeatureCursor fCursor = fcls.Search(null, false);
            IUniqueValueRenderer pRender = new UniqueValueRenderer();
            pRender.FieldCount = 1;
            pRender.set_Field(0, "gid");
            //IFeature pFeat = fCursor.NextFeature();
            //int index = pFeat.Fields.FindField("BSM");
            RgbColor pRGB = new RgbColor();
            pRGB.Red = LineColor[0];
            pRGB.Green = LineColor[1];
            pRGB.Blue = LineColor[2];
            IColor pLineColor = new RgbColor()
            {
                Red = LineColor[0],
                Green = LineColor[1],
                Blue = LineColor[2]
            };
            ICartographicLineSymbol pCartoLineSymbol = new CartographicLineSymbol();
            pCartoLineSymbol.Width = LineWidth;
            pCartoLineSymbol.Color = pLineColor;
            ISimpleFillSymbol pSmplFillSymbol = new SimpleFillSymbol();
            IColor pRgbClr = new RgbColor()
            {
                Red = fillColor[0],
                Green = fillColor[1],
                Blue = fillColor[2]
            };
            pRgbClr.Transparency = fillColor[3];
            IFillSymbol pFillSymbol = pSmplFillSymbol;
            pFillSymbol.Color = pRgbClr;
            pFillSymbol.Outline = pCartoLineSymbol;
            for (int i = 0; i < bsms.Count; i++)
            {
                pRender.AddValue(bsms[i], bsms[i], pSmplFillSymbol as ISymbol);
            }
           /* while (pFeat != null)
            {
                string valuestr = pFeat.get_Value(index);
                
                pFeat = fCursor.NextFeature();
            }*/
            geoLayer.Renderer = pRender as IFeatureRenderer;
        }
    }
}
